import { CANVAS_WIDTH, CANVAS_HEIGHT } from '../utils/constants.js'

// 玩家类
export default class Player {
  static WIDTH = 50
  static HEIGHT = 60
  static SPEED = 5

  constructor() {
    this.width = Player.WIDTH
    this.height = Player.HEIGHT
    this.speed = Player.SPEED
    this.color = '#0088ff'
    this.isMovingLeft = false
    this.isMovingRight = false
    this.isMovingUp = false
    this.isMovingDown = false
    this.isShooting = false

    // 道具相关属性
    this.weaponLevel = 1 // 武器等级，默认为1
    this.hasShield = false // 是否有护盾
    this.weaponTimer = null // 武器升级定时器
    this.shieldTimer = null // 护盾定时器

    // 加载图像
    this.image = new Image()
    this.image.src =
      ''

    this.reset()
  }

  reset() {
    this.x = CANVAS_WIDTH / 2 - this.width / 2
    this.y = CANVAS_HEIGHT - this.height - 20

    // 重置道具相关属性
    this.weaponLevel = 1
    this.hasShield = false

    // 清除定时器
    if (this.weaponTimer) {
      clearTimeout(this.weaponTimer)
      this.weaponTimer = null
    }

    if (this.shieldTimer) {
      clearTimeout(this.shieldTimer)
      this.shieldTimer = null
    }
  }

  update() {
    // 更新玩家位置
    if (this.isMovingLeft && this.x > 0) {
      this.x -= this.speed
    }
    if (this.isMovingRight && this.x < CANVAS_WIDTH - this.width) {
      this.x += this.speed
    }
    if (this.isMovingUp && this.y > 0) {
      this.y -= this.speed
    }
    if (this.isMovingDown && this.y < CANVAS_HEIGHT - this.height) {
      this.y += this.speed
    }
  }

  render(ctx) {
    ctx.drawImage(this.image, this.x, this.y, this.width, this.height)

    // 根据武器等级绘制不同颜色的引擎光芒
    this.renderEngineGlow(ctx)
  }

  // 绘制引擎光芒，根据武器等级显示不同颜色
  renderEngineGlow(ctx) {
    let glowColor

    switch (this.weaponLevel) {
      case 1:
        glowColor = 'rgba(255, 255, 0, 0.7)' // 黄色
        break
      case 2:
        glowColor = 'rgba(255, 165, 0, 0.7)' // 橙色
        break
      case 3:
        glowColor = 'rgba(255, 0, 0, 0.7)' // 红色
        break
      default:
        glowColor = 'rgba(255, 255, 0, 0.7)'
    }

    // 绘制引擎光芒
    ctx.beginPath()
    ctx.moveTo(this.x + this.width / 2 - 10, this.y + this.height)
    ctx.lineTo(this.x + this.width / 2 - 5, this.y + this.height + 10)
    ctx.lineTo(this.x + this.width / 2 + 5, this.y + this.height + 10)
    ctx.lineTo(this.x + this.width / 2 + 10, this.y + this.height)
    ctx.fillStyle = glowColor
    ctx.fill()
  }
}
